<template>
  <div class="config-container">
    <div class="preview-wrapper">
      <div class="preview-box" :style="{}">
        <div :class="['content', position]" :style="{ backgroundColor: bgColor, color: textColor }">
          <marquee :scrollamount="scrollSpeed" :direction="getDirection">{{ marqueeText }}</marquee>
        </div>
      </div>
    </div>
    <div class="controls">
      <div class="control-group bg-color-group">
        <label>背景颜色:</label>
        <div class="buttons">
          <button :class="{ active: bgColor === 'red' }" @click="setBackgroundColor('red')">红色</button>
          <button :class="{ active: bgColor === 'gray' }" @click="setBackgroundColor('gray')">灰色</button>
          <button :class="{ active: bgColor === 'yellow' }" @click="setBackgroundColor('yellow')">黄色</button>
          <button :class="{ active: bgColor === 'lightgray' }" @click="setBackgroundColor('lightgray')">浅灰</button>
          <button :class="{ active: colorType === 'background' && showPicker }" class="custom-button" @click="showColorPicker('background')">+ 自定义</button>
        </div>
      </div>
      <div class="control-group text-color-group">
        <label>文字颜色:</label>
        <div class="buttons">
          <button :class="{ active: textColor === 'red' }" @click="setTextColor('red')">红色</button>
          <button :class="{ active: textColor === 'gray' }" @click="setTextColor('gray')">灰色</button>
          <button :class="{ active: textColor === 'yellow' }" @click="setTextColor('yellow')">黄色</button>
          <button :class="{ active: textColor === 'lightgray' }" @click="setTextColor('lightgray')">浅灰</button>
          <button :class="{ active: colorType === 'text' && showPicker }" class="custom-button" @click="showColorPicker('text')">+ 自定义</button>
        </div>
      </div>
      <div class="control-group speed-group">
        <label>滚动速度:</label>
        <input type="range" min="1" max="100" v-model="scrollSpeed" class="speed-slider">
        <span class="speed-value">{{ scrollSpeed }} px/s</span>
      </div>
      <div class="control-group position-group">
        <label>显示位置:</label>
        <div class="buttons">
          <button :class="{ active: position === 'top' }" @click="setPosition('top')">上</button>
          <button :class="{ active: position === 'center' }" @click="setPosition('center')">中</button>
          <button :class="{ active: position === 'bottom' }" @click="setPosition('bottom')">下</button>
          <button :class="{ active: position === 'left' }" @click="setPosition('left')">左</button>
          <button :class="{ active: position === 'right' }" @click="setPosition('right')">右</button>
        </div>
      </div>
    </div>
    <ColorPicker v-if="showPicker" @color-picked="applyCustomColor" @hide-picker="showPicker = false" />
  </div>
</template>

<script>
import ColorPicker from './ColorPicker.vue';

export default {
  name: 'ConfigurableBox',
  components: {
    ColorPicker
  },
  data() {
    return {
      bgColor: 'red',
      textColor: 'yellow',
      scrollSpeed: 20,
      position: 'bottom',
      showPicker: false,
      colorType: '',
      marqueeText: '配置内容'
    };
  },
  methods: {
    setBackgroundColor(color) {
      this.bgColor = color;
    },
    setTextColor(color) {
      this.textColor = color;
    },
    showColorPicker(type) {
      this.colorType = type;
      this.showPicker = true;
    },
    applyCustomColor(color) {
      if (this.colorType === 'background') {
        this.bgColor = color;
      } else if (this.colorType === 'text') {
        this.textColor = color;
      }
      this.showPicker = false;
    },
    setPosition(position) {
      this.position = position;
    }
  },
  computed: {
    getDirection() {
      if (this.position === 'left' || this.position === 'right') {
        return 'down';
      }
      return 'left';
    }
  }
};
</script>

<style scoped>
.config-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.preview-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.preview-box {
  border: 2px solid #000;
  width: 100%;
  height: 130px;
  position: relative;
  box-sizing: border-box;
  overflow: hidden; /* 确保文字不会溢出 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.content {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
}

.content.top {
  top: 0;
  left: 0;
  right: 0;
  padding-bottom: 10px;
  justify-content: center;
  align-items: flex-start;
}

.content.center {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  padding: 5px 0px;
  justify-content: center;
  align-items: center;
}

.content.bottom {
  bottom: 0;
  left: 0;
  right: 0;
  padding-top: 10px;
  justify-content: center;
  align-items: flex-end;
}

.content.left {
  top: 0;
  bottom: 0;
  left: 0;
  padding-right: 10px;
  height: 100%;
  writing-mode: vertical-rl;
  justify-content: center;
  align-items: flex-start;
}

.content.right {
  top: 0;
  bottom: 0;
  right: 0;
  padding-left: 10px;
  height: 100%;
  writing-mode: vertical-rl;
  justify-content: center;
  align-items: flex-end;
}

marquee {
  font-size: 14px; /* 调整文字大小 */
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  display: block;
  height: 100%;
}

.controls {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.control-group {
  margin-bottom: 20px;
  text-align: center;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.bg-color-group {
  background-color: #f2f8ff;
}

.text-color-group {
  background-color: #f9f2ff;
}

.speed-group {
  background-color: #f2fff2;
}

.position-group {
  background-color: #fff9f2;
}

.control-group label {
  font-weight: bold;
  display: block;
  margin-bottom: 10px;
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.buttons button {
  margin: 5px;
  padding: 10px 15px;
  font-size: 14px;
  cursor: pointer;
  border: none;
  border-radius: 5px;
  background-color: #f0f0f0;
  transition: background-color 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.buttons button.active,
.buttons button:hover {
  background-color: #add8e6;
}

.custom-button {
  padding: 10px 25px; /* 扩大自定义按钮的宽度 */
}

.speed-slider {
  width: 80%;
}

.speed-value {
  display: inline-block;
  margin-left: 10px;
  font-weight: bold;
}
</style>
